package com.jjb.www.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.hjq.bar.TitleBar;
import com.hjq.base.FragmentPagerAdapter;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppFragment;
import com.jjb.www.bean.BannerBean;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.ui.adapter.AgencyAdapter;
import com.jjb.www.ui.adapter.TabAdapter;
import com.jjb.www.ui.fragment.CourseListFragment;
import com.jjb.www.ui.fragment.JPCourseListFragment;
import com.jjb.www.widget.CommonRefreshView;

import java.util.ArrayList;
import java.util.List;
/**
 *    author : Android CRG
 *    time   : 2018/10/18
 *    desc   : 精品课程
 */
public class PremiumCoursesActivity extends AppActivity implements TabAdapter.OnTabListener, ViewPager.OnPageChangeListener{

    private RecyclerView mTabView;
    private ViewPager mViewPager;
    private TabAdapter mTabAdapter;
    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_premium_courses;
    }

    @Override
    protected void initView() {
        mTabView = findViewById(R.id.rv_home_tab);
        mViewPager = findViewById(R.id.vp_home_pager);

        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mViewPager.addOnPageChangeListener(this);
        mTabAdapter = new TabAdapter(getActivity(), TabAdapter.TAB_MODE_DESIGN, false);
        mTabView.setAdapter(mTabAdapter);
        mTabAdapter.setOnTabListener(this);
    }

    @Override
    protected void initData() {
        //课程列表分类
        getCurriculumSort();
    }
    private void getCurriculumSort() {
        EasyHttp.get(this)
                .api(new CurriculumSortApi())
                .request(new HttpCallback<HttpList<CurriculumSortApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpList<CurriculumSortApi.Bean> data) {
                        mPagerAdapter.addFragment(JPCourseListFragment.newInstance("0"), "推荐");
                        mTabAdapter.addItem("推荐");
                        for (CurriculumSortApi.Bean bean:data.getData()) {
                            mPagerAdapter.addFragment(JPCourseListFragment.newInstance(bean.getId()+""), bean.getTitle());
                            mTabAdapter.addItem(bean.getTitle());
                        }
                        mViewPager.setAdapter(mPagerAdapter);

                    }

                });
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mTabAdapter == null) {
            return;
        }
        mTabAdapter.setSelectedPosition(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onTabSelected(RecyclerView recyclerView, int position) {
        mViewPager.setCurrentItem(position);
        return true;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mViewPager.setAdapter(null);
        mViewPager.removeOnPageChangeListener(this);
        mTabAdapter.setOnTabListener(null);
    }
}